﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Mobile;
using ESRI.ArcGIS.Mobile.MobileServices;
using ESRI.ArcGIS.Mobile.Sketch;
using ESRI.ArcGIS.Mobile.Geometries;
using ESRI.ArcGIS.Mobile.MapActions;

namespace Handrise.CDCable.Mobile.GISControl
{
    public partial class UCGIS : UserControl
    {

        /// <summary>
        /// 需要显示的点
        /// </summary>
        private Multipoint allcoordinates;
        /// <summary>
        /// 设备名称
        /// </summary>
        private List<string> pointname;


        /// <summary>
        /// 当前屏幕内的点
        /// </summary>
        private Multipoint showcoordinates;
        /// <summary>
        /// 当前屏幕内的设备名称
        /// </summary>
        private List<string> showpointname;


        /// <summary>
        /// 需要显示的线
        /// </summary>
        private List<Polyline> alllinecoordinates;


        /// <summary>
        /// 线名称显示点
        /// </summary>
        private Multipoint linenameport;
        /// <summary>
        /// 线路名称
        /// </summary>
        private List<string> linename;
         /// <summary>
        ///显示点名称
        /// </summary>
        //   private IList<string> pointname;
        /// <summary>
        /// 缓存数据路径
        /// </summary>
        private Pen pointp;
        private Pen pointline;
        private SolidBrush pointsb;
        private bool showDeviceName=false;

        /// <summary>
        /// 获取缓存数据地址
        /// </summary>
        public string CacheStoragePath
        {
            set
            {
                MScatch.CacheStoragePath = value;
            }
            get
            {
                return MScatch.CacheStoragePath;
            }
        }
        /// <summary>
        /// 地图缩放
        /// </summary>
        public double Scale
        {
            set
            {
                map.Scale = value;
            }
            get
            {
                return map.Scale;
            }
        }

        /// <summary>
        /// 显示名称
        /// </summary>
        public bool ShowDeviceName
        {
            set
            {
                showDeviceName = value;
            }
            get
            {
                return showDeviceName;
            }
        }


        /// <summary>
        /// 需要绘制的点
        /// </summary>
        public List<PDGPS> ListPoint { get; set; }


        public UCGIS()
        {
            InitializeComponent();
            ListPoint = new List<PDGPS>();
            pointp = new Pen(Color.Red  );
            pointline = new Pen(Color.BlueViolet);
            pointline.Width = 2;
            pointsb = new SolidBrush(Color.Red);
      
            allcoordinates = new Multipoint();
            alllinecoordinates=new List<Polyline> ();
            pointname = new List<string>();
            linenameport = new Multipoint();
            linename = new List<string>();
            showcoordinates=new Multipoint();
            showpointname= new List<string>();

        }
        /// <summary>
        /// 清空显示点
        /// </summary>
        public bool clearpaintpont()
        {
            try
            {
                allcoordinates.Clear();
                alllinecoordinates.Clear();
                pointname.Clear();
                linenameport.Clear();
                linename .Clear();
                showcoordinates.Clear();
                showpointname.Clear();
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
                return false;
            }
            return true;

        }

        /// <summary>
        /// 清空显示点
        /// </summary>
        public bool closecatch()
        {
            try
            {
               allcoordinates.Clear();
               alllinecoordinates.Clear();
               pointname.Clear();
               linenameport.Clear();
               linename.Clear();
               showcoordinates.Clear();
               showpointname.Clear();
               MScatch.Close();
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
                return false;
            }
            return true;

        }
        /// <summary>
        /// 显示lable
        /// </summary>
        /// <param name="show"></param>
        /// <returns></returns>
        public bool showlab(bool show)
    {
        foreach (MapLayer mapLayer in map.MapLayers)
        {
         
                foreach (MapLabelClass mlc in mapLayer.MapLabelClasses)
                {
                    mlc.Visible = show;
                }
            
        }
        return true;
    }
                /// <summary>
        ///  
        /// </summary>
        /// <param name="show"></param>
        /// <returns></returns>
        public bool showlayer()
    {
        if (Scale <= 10000)
            showDeviceName = true;
        else
            showDeviceName = false;
        foreach (MapLayer mapLayer in map.MapLayers)
        {

            switch (mapLayer.Name)
            {
                // case "FOREVERGEO_shuiximian_1":
                //     { if map.Scale>}
                case "FOREVERGEO_quxianjie_1":  //区县
                    {
                        if (Scale >= 30000)
                        {
                            foreach (MapLabelClass mlc in mapLayer.MapLabelClasses)
                            {
                                mlc.Visible = true;
                            }
                        }
                        else
                            foreach (MapLabelClass mlc in mapLayer.MapLabelClasses)
                            {
                                mlc.Visible = false;
                            }
                        break;
                    }
                // case "FOREVERGEO_jumindimian_1":
                //     {}
                case "FOREVERGEO_jiebanmian_1":
                    {
                        if (Scale <= 30000)
                        {
                            foreach (MapLabelClass mlc in mapLayer.MapLabelClasses)
                            {
                                mlc.Visible = true;
                            }
                        }
                        else
                            foreach (MapLabelClass mlc in mapLayer.MapLabelClasses)
                            {
                                mlc.Visible = false;
                            }
                        break;

                    }
                //  case "FOREVERGEO_ditubianjie_1":
                //    { }
                case "FOREVERGEO_daolumian_1":
                    {
                        if (Scale <= 30000)
                        {
                            foreach (MapLabelClass mlc in mapLayer.MapLabelClasses)
                            {
                                mlc.Visible = true;
                            }
                        }
                        else
                            foreach (MapLabelClass mlc in mapLayer.MapLabelClasses)
                            {
                                mlc.Visible = false;
                            }
                        break;
                    }
                // case "FOREVERGEO_dlzxx_1":
                //     { }
                case "FOREVERGEO_daoluzhongxian_1":
                    {
                        if (Scale <= 30000)
                        {
                            foreach (MapLabelClass mlc in mapLayer.MapLabelClasses)
                            {
                                mlc.Visible = true;
                            }
                        }
                        else
                            foreach (MapLabelClass mlc in mapLayer.MapLabelClasses)
                            {
                                mlc.Visible = false;
                            }
                        break;
                    }
                // case "FOREVERGEO_UCAD道路_1":
                //     { }

                case "FOREVERGEO_jumindizhuji_1":
                    {
                        if (Scale <= 30000)

                            mapLayer.Visible = true;
                        else
                            mapLayer.Visible = false;
                        if (Scale <= 10000)
                        {
                            foreach (MapLabelClass mlc in mapLayer.MapLabelClasses)
                            {
                                mlc.Visible = true;
                            }
                        }
                        else
                            foreach (MapLabelClass mlc in mapLayer.MapLabelClasses)
                            {
                                mlc.Visible = false;
                            }
                        break;
                    }

               
                case "FOREVERGEO_shuixizhuji_1":
                    {
                        if (Scale <= 30000)
                        {
                            foreach (MapLabelClass mlc in mapLayer.MapLabelClasses)
                            {
                                mlc.Visible = true;
                            }
                        }
                        else
                            foreach (MapLabelClass mlc in mapLayer.MapLabelClasses)
                            {
                                mlc.Visible = false;
                            }
                        break;
                    }

                case "FOREVERGEO_UCAD注记_1":
                    {
                        if (Scale <= 30000)

                            mapLayer.Visible = true;
                        else
                            mapLayer.Visible = false;
                        if (Scale <= 10000)
                        {
                            foreach (MapLabelClass mlc in mapLayer.MapLabelClasses)
                            {
                                mlc.Visible = true;
                            }
                        }
                        else
                            foreach (MapLabelClass mlc in mapLayer.MapLabelClasses)
                            {
                                mlc.Visible = false;
                            }
                        break;
                     
                    }

            }
         //   map.Scale
       
           
        }
 

        return true;
    }

           

        public bool atpoint(double x, double y)
        {
            try
            {
                Coordinate coord = new Coordinate();
                coord.X = x;
                coord.Y = y;
                map.CenterAt(coord);
                return true;
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
                return false;
            }
        }
        /// <summary>
        /// 获取缓存地图
        /// </summary>
        public bool Getcatchmap()
        {
            if (MScatch.CacheStoragePath != null)
            {
                try
                {
                    MScatch.Open();
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message);
                    return false;
                }

                map.CurrentMapAction = panMapAction1;
                foreach (MapLayer mapLayer in map.MapLayers)
                {
                    foreach (MapLabelClass mlc in mapLayer.MapLabelClasses)
                    {
                        mlc.Visible = false;
                    }
                }

                return true;
            }
            else
                return false;

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>


        private void map_Paint(object sender, ESRI.ArcGIS.Mobile.MapPaintEventArgs e)
        {
            int n;
            try
            {
                e.Display.DrawMultipoint(pointp, pointsb, 4, allcoordinates);
                if (showDeviceName)
                {
                    n = 0;
                   foreach ( Coordinate cd in showcoordinates.Coordinates)
                    {
                        e.Display.DrawText(showpointname[n], new Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold), new SolidBrush(Color.Red), cd, TextAlignment.TopLeft);
                        n = n + 1;
                    }
                }
                foreach (Polyline pdevice in alllinecoordinates)
              {
                    if (alllinecoordinates.Count > 0)
                     e.Display.DrawPolyline(pointline, pdevice);
                }
            }
            catch (Exception error)
            {
              //  MessageBox.Show(error.Message);

            }

        }

        public bool changepoint()
        {
            int i, j,n;
            double dx, dy;
            foreach (PDGPS pg in ListPoint)
            {
                try
                {
                    if (pg.GPSInfo.Length < 20)
                    //点
                    {
                        String[] sArray = pg.GPSInfo.Split(';');
                        foreach (string stri in sArray)
                        {
                            i = stri.IndexOf(",");
                            if (i != -1)
                            {
                                j = stri.Length;
                                dx = Convert.ToDouble(stri.Substring(0, i));
                                dy = Convert.ToDouble(stri.Substring(i + 1, j - i - 1));
                                Coordinate coord = new Coordinate();
                                coord.X = dx;
                                coord.Y = dy;
                                allcoordinates.Coordinates.Add(coord);
                                pointname.Add(pg.Devicename);
                            }

                        }
                    }

                    else //线
                    {
                        Polyline Polyline = new Polyline();
                        String[] sArray = pg.GPSInfo.Split(';');
                        n = 0;
                        foreach (string stri in sArray)
                        {
                         
                            i = stri.IndexOf(",");
                            if (i != -1)
                            {
                                j = stri.Length;
                                dx = Convert.ToDouble(stri.Substring(0, i));
                                dy = Convert.ToDouble(stri.Substring(i + 1, j - i - 1));
                                Coordinate coord = new Coordinate();
                                coord.X = dx;
                                coord.Y = dy;
                                Polyline.AddCoordinate(coord);
                                if (n == 0)
                                {
                                    linenameport.AddCoordinate(coord);
                                    linename.Add(pg.Devicename);
                                    n = n + 1;
                                }
                            }
                        }
                        alllinecoordinates.Add(Polyline);
                    }
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message);
                    return false;

                }
            }

            return true;

        }

        private void map_LayersDrawingCompleted(object sender, EventArgs e)
        {

        }

        private void map_IsValidChanged(object sender, EventArgs e)
        {
          
        }

        private void map_ExtentChanged(object sender, EventArgs e)
        {
            Coordinate coord1 = map.ToMap(0, 0);
            Coordinate coord2 = map.ToMap(230, 240);
            double x1 = coord1.X;
            double x2 = coord2.X;
            double y2 = coord2.Y;
            double y1 = coord1.Y;
            showcoordinates.Clear();
            showpointname.Clear();
            int n = 0;
            foreach (Coordinate cd in allcoordinates.Coordinates)
            {
                if ((cd.X > x1) && (cd.X < x2) && (cd.Y > y2) && (cd.Y < y1))
                {
                    showcoordinates.AddCoordinate(cd);
                    showpointname.Add(pointname[n]);
               }
                n = n + 1;
            }
           
        }

    
    }
}
    /// <summary>
    /// 现实点转换
    /// </summary>
    /// 


        public class PDGPS
        {
            public string DeviceGPSID { get; set; }
            /// <summary>
            /// 设备编码
            /// </summary>
            public string DeviceCode { get; set; }
            /// <summary>
            /// 设备类型
            /// </summary>
            public string DeviceType { get; set; }
            /// <summary>
            /// 经纬度信息,格式经纬度通过","隔开，每个点通过";"隔开,每段通过"|"分开
            /// 如：经度,纬度;经度,纬度|经度,纬度;经度,纬度...
            /// </summary>
            public string GPSInfo { get; set; }
            /// <summary>
            /// 本地经纬度,格式同上
            /// </summary>
            public string LocalGPSInfo { get; set; }
            /// <summary>
            /// 设备名称
            /// </summary>
            public string Devicename { get; set; }


        }
  
